package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.core.Result;
import com.dongdongshop.core.ResultEnum;
import com.dongdongshop.pojo.cart.Cart;
import com.dongdongshop.pojo.user.TbUser;
import com.dongdongshop.pojo.vo.OrderItemVo;
import com.dongdongshop.service.cart.CartService;
import com.dongdongshop.service.order.OrderService;
import com.dongdongshop.util.CookieUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
* 提交订单
* */
@Controller
@RequestMapping("order")
public class OrderController {

    private final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @Reference
    private OrderService orderServiceI;

    @Reference
    private CartService cartService;

    @RequestMapping("homeOrderEvaluate")
    public String homeOrderEvaluate(){
        return "home-order-evaluate";
    }

    @RequestMapping("updatePwd")
    public String updatePwd(){
        return "updatePwd";
    }

    @RequestMapping("toCart")
    public String toCart(Model model, HttpServletResponse response, HttpServletRequest request){
        //获取登录用户
        Subject subject = SecurityUtils.getSubject();
        TbUser user = (TbUser)subject.getPrincipal();
        //判断用户是否登录
        if (user == null){
            //如果没有从cookie中取购物车信息
            String cartList = CookieUtils.getCookieValue(request, "cartList", "UTF-8");
            List<Cart> cartListCookie = JSONObject.parseArray(cartList, Cart.class);
            model.addAttribute("cartList",cartListCookie);
        }else {
            //登录就从redis中获取购物车中
            List<Cart> cartListRedis = cartService.getCartListFromRedis(user.getUsername());
            model.addAttribute("cartList", cartListRedis);
        }
        model.addAttribute("user",user);
        return "cart/cartList";
    }

    //跳转到用户的个人信息页面
    @RequestMapping("toHomeIndex")
    public String toHomeIndex(Long itemId,Model model){
        TbUser principal = (TbUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",principal);
        return "userhome/home-index";
    }
    //跳转到用户的个人信息页面
    @RequestMapping("toHomeSettingAddress")
    public String toHomeSettingAddress(Long itemId,Model model){
        TbUser principal = (TbUser) SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("user",principal);
        return "userhome/home-setting-address";
    }

    //跳转结算页面
    @RequestMapping("togetOrderInfo")
    public String togetOrderInfo(Long itemId,Model model,HttpServletRequest request){
        TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
        //判断用户是否登录
        if (user == null){
            //如果没有从cookie中取购物车信息
            String cartList = CookieUtils.getCookieValue(request, "cartList", "UTF-8");
            List<Cart> cartListCookie = JSONObject.parseArray(cartList, Cart.class);
            model.addAttribute("cartList",cartListCookie);
        }else {
            //登录就从redis中获取购物车中
            List<Cart> cartListRedis = cartService.getCartListFromRedis(user.getUsername());
            model.addAttribute("cartList", cartListRedis);
        }
        model.addAttribute("user",user);
        return "order/getOrderInfo";
    }

    //提交订单
    @RequestMapping("submitOrder/{addressId}/{totalPrice}")
    @ResponseBody
    public String submitOrder(@PathVariable("addressId") Long addressId,@PathVariable("totalPrice") Long totalPrice){
        try {
            //获取用户
            TbUser user = (TbUser) SecurityUtils.getSubject().getPrincipal();
            orderServiceI.submitOrder(addressId,totalPrice,user);
            return "提交订单成功";
        }catch (Exception e){
            logger.error("提交订单失败: {}",e);
            return "error";
        }
    }

    /**
     * 获取没有评论的订单
     * @return
     */
    @RequestMapping("getOrderStatus")
    @ResponseBody
    public Result getOrderStatus(){
        try {
            TbUser principal = (TbUser) SecurityUtils.getSubject().getPrincipal(); // 取出登录对象
            List<OrderItemVo> orderItemVos = orderServiceI.getOrderStatus(principal.getId());

            return Result.response(ResultEnum.SUCCESS).setData(orderItemVos);
        }catch (Exception e){
            logger.error("获取没有评论的订单:",e);
            return Result.response(ResultEnum.ERROR);
        }

    }
}
